<template>
  <div class="app-container">
    <el-card class="box-card" shadow="never">
      <el-form :inline="true" :model="dataForm">
        <el-form-item label="公司名称">
          <el-autocomplete
            clearable
            v-model="dataForm.companyName"
            :fetch-suggestions="querySearch"
            placeholder="请输入公司名称"
            :trigger-on-focus="false"
            @select="handleSelect"
          ></el-autocomplete>
        </el-form-item>
        <el-form-item label="单位类型">
          <el-select
            v-model="dataForm.companyTypeId"
            placeholder="请选择公司类型"
            clearable
            filterable
            style="width:100%"
          >
            <el-option
              v-for="item in companyTypeList"
              :key="item.id"
              :label="item.companyTypeName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="预警时间">
          <el-date-picker
            v-model="dataForm.dateTime"
            type="daterange"
            class="time"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            :picker-options="pickerOptionsItem"
          ></el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="box-card" shadow="never">
      <!-- 数据展示 -->
      <el-table
        :data="tableData"
        :header-cell-style="{background:'#f0f3fa',textAlign:'center'}"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        :cell-style="{textAlign:'center'}"
      >
        <el-table-column prop="companyName" label="公司名称"></el-table-column>
        <el-table-column prop="companyTypeName" label="公司类型"></el-table-column>
        <el-table-column label="用水量(吨/年)">
          <template slot-scope="scope">
            <span class="bule" @click="handleSee(scope.row)">{{ scope.row.totalWater}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="waterYear" label="预警阈值(吨/年)"></el-table-column>
        <el-table-column prop="waterTypeName" label="用水类型"></el-table-column>
        <!-- <el-table-column prop="address" label="预警描述"></el-table-column> -->
        <el-table-column prop="warningDate" label="预警时间"></el-table-column>
        <el-table-column label="是否超预警">
          <template>
            <span>是</span>
          </template>
        </el-table-column>
        <!-- <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" content="详情">
              <i class="el-icon-info" @click="handleEdit(scope.row)"></i>
            </el-tooltip>
          </template>
        </el-table-column>-->
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="dataForm.page"
        :page-sizes="[10, 20, 40, 60]"
        :page-size="dataForm.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
    <!-- 预警详情 -->
    <warningDetails ref="warningDetails"></warningDetails>
  </div>
</template>

<script>
import { getCompanyType } from '@/api/base'
import warningDetails from './component/warningDetails.vue'
import { waterWarningRecordList } from '@/api/other'
export default {
  // 组件注册
  components: {
    warningDetails,
  },
  data() {
    return {
      dataForm: {
        companyId: '',
        companyName: '',
        companyTypeId: '',
        dateTime: [this.ShowDate(90), this.ShowDate(0)], // mixins中
        page: 1,
        pageSize: 10,
      },
      companyList: [],
      companyTypeList: [],
      tableData: [],
      total: 0,
    }
  },
  mounted() {
    this.getCompanyTypeReq() // 公司类型
    this.waterWarningRecordListReq()
  },
  methods: {
    // querySearch()  该方法在混入中
    getCompanyTypeReq() {
      getCompanyType().then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        this.companyTypeList = data
      })
    },
    waterWarningRecordListReq() {
      let data = {
        companyId: this.dataForm.companyId,
        companyTypeId: this.dataForm.companyTypeId,
        createDateEnd: this.dataForm.dateTime[1] + ' 23:59:59',
        createDateStart: this.dataForm.dateTime[0] + ' 00:00:00',
        page: this.dataForm.page,
        pageSize: this.dataForm.pageSize,
      }
      waterWarningRecordList(data).then((res) => {
        const { code, data, msg } = res
        if (code !== '0000') return this.$message.error(msg)
        const { total, list } = data
        this.total = total
        this.tableData = list
      })
    },
    // 预警描述
    handleSee({ id }) {
      this.$refs.warningDetails.init(id)
    },
    handleSelect(item) {
      this.dataForm.companyId = item.id
    },
    onSubmit() {
      this.dataForm.page = 1
      this.waterWarningRecordListReq()
    },
    // 监听pageSize改变事件
    handleSizeChange(newSize) {
      this.dataForm.pageSize = newSize
      this.waterWarningRecordListReq()
    },
    // 监听 页面值 改变的事件
    handleCurrentChange(newPage) {
      this.dataForm.page = newPage
      this.waterWarningRecordListReq()
    },
  },
}
</script>

<style lang='scss' scoped>
</style>
